package cn.kooki.app.duobao.util.manager;

import android.content.Context;

import com.umeng.analytics.MobclickAgent;

import cn.jpush.android.api.JPushInterface;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.core.MainFactory;
import cn.kooki.app.duobao.data.Bean.User.LoginResponse;
import cn.kooki.app.duobao.data.Bean.User.UserInfo;

import cn.kooki.app.duobao.data.Bean.base.BaseResponse;
import cn.kooki.app.duobao.data.bus.NewStateEvent;
import cn.kooki.app.duobao.util.SharePreferencesUtils;

import de.greenrobot.event.EventBus;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by xiong on 2016/1/26.
 */
public class LoginManager {

    private static LoginManager manager;

    public interface LoginHandler {
        void onLoginStart();

        void onLoginComplete();

        void onLoginFair(String msg);

        void onOtherLoginComplete();
    }


    private LoginHandler loginHandler;


    public static void checkIsNew(final String uid) {
        MainFactory.getApiInstance().IsOneBonus(uid, 1, new Callback<BaseResponse>() {
            @Override
            public void success(BaseResponse baseResponse, Response response) {
                NewStateEvent event = new NewStateEvent();
                if (baseResponse.getStatus() == 0) {
                    event.isNew = true;
                }
                event.uid = uid;
                EventBus.getDefault().post(event);
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }


    public void doLogin(final Context context, String username, String pass, LoginHandler handler) {

        this.loginHandler = handler;
        if (loginHandler != null) {
            loginHandler.onLoginStart();
        }
        MainFactory.getApiInstance().login(username, pass, 1, 4, new Callback<LoginResponse>() {
            @Override
            public void success(LoginResponse baseResponse, Response response) {

                if (baseResponse.getStatus() == 0) {
                    UserInfo userInfo = baseResponse.getData();
                    SharePreferencesUtils.putBoolean(context, SharePreferencesUtils.ISLOGIN, true);
                    SharePreferencesUtils.putUserinfo(context, userInfo);
                    JPushInterface.setAlias(context.getApplicationContext(), userInfo.getUid(), null);
                    MobclickAgent.onProfileSignIn(userInfo.getUid());
                    LoginManager.checkIsNew(userInfo.getUid());
                    if (loginHandler != null) {
                        loginHandler.onLoginComplete();
                    }
                } else {
                    if (loginHandler != null) {
                        loginHandler.onLoginFair(baseResponse.getMsg());
                    }
                }
            }

            @Override
            public void failure(RetrofitError error) {
                if (loginHandler != null) {
                    loginHandler.onLoginFair(context.getResources().getString(R.string.network_error));
                }
            }
        });
    }


}

